<?php
if(!defined('IN_refrigeratory')) {
	exit('Access Denied');
}

define("GROUP_1", 1);
define("GROUP_2", 2);
define("GROUP_3", 3);
define("GROUP_4", 4);
define("GROUP_5", 5);


//数据库连接
include_once('./config.php');
include_once('./source/class_mysql.php');

if(!get_magic_quotes_gpc())   {
    $_REQUEST = array_map( 'addslashes',   $_REQUEST); 
} 

$_SGLOBAL = array();
$_SGLOBAL['db'] = new dbstuff;
$_SGLOBAL['db']->charset = $_SC['dbcharset'];
$_SGLOBAL['db']->connect($_SC['dbhost'], $_SC['dbuser'], $_SC['dbpw'], $_SC['dbname'], $_SC['pconnect']);

if(!isset($_SESSION)){
    session_start();
} 

// 查询SQL语句，并返回结果集格式化JSON
// 结果格式：{"success":true, "count":1, "data"：{...}}
function get_json_from_sql($sql, $bfields=array()){
	global $_SGLOBAL;
	$bfields = is_array($bfields) ? $bfields : array($bfields);
	
	$query = $_SGLOBAL['db']->query($sql);
	$result = array();
	$result["success"] = true;
	$result["count"] = $_SGLOBAL['db']->num_rows($query);
	$result["data"] = array();
	if ($_SGLOBAL['db']->num_rows($query) != 0){
		while($row = $_SGLOBAL['db']->fetch_array($query)){
			foreach($row as $field => $value){
				if (in_array($field, $bfields))
					$row[$field] = $row[$field] == "1" ? true : false;
				else if (is_null($value))
					$row[$field] = "";
			}
			$result["data"][] = $row;
		}		
	}
	return json_encode($result);
}

// 根据SQL获取结果数组
function get_array_from_sql($sql, $bfields=array()){
	global $_SGLOBAL;
	$query = $_SGLOBAL['db']->query($sql);
	$res = array();
	if ($_SGLOBAL['db']->num_rows($query) != 0){
		while($row = $_SGLOBAL['db']->fetch_array($query)){
			foreach($row as $field => $value){
				if (in_array($field, $bfields))
					$row[$field] = $row[$field] == "1" ? true : false;
				else if (is_null($value))
					$row[$field] = "";
			}
			array_push($res, $row);
		}
	}
	return $res;
}

// 根据SQL获取一行结果数组
function get_row_array_from_sql($sql){
	global $_SGLOBAL;
	$query = $_SGLOBAL['db']->query($sql);
	$res = array();
	if ($_SGLOBAL['db']->num_rows($query) != 0){
		if($row = $_SGLOBAL['db']->fetch_array($query)){
			$res = $row;
		}
	}
	return $res;
}

// 根据SQL获取单元
function get_value_from_sql($sql, $field){
	global $_SGLOBAL;
	$query = $_SGLOBAL['db']->query($sql);
	if ($_SGLOBAL['db']->num_rows($query) == 1){
		$row= $_SGLOBAL['db']->fetch_array($query);
		return $row[$field];
	}
	else{
		return "";
	}
}

// 获取SQL获取行数
function get_rownum_with_sql($sql){
	global $_SGLOBAL;
	$query = $_SGLOBAL['db']->query($sql);
	return $_SGLOBAL['db']->num_rows($query);
}

// 执行插入操作SQL并返回id
function execute_insert_sql($sql){	
	global $_SGLOBAL;
	$query = $_SGLOBAL['db']->query($sql);
	return $_SGLOBAL['db']->insert_id();
}

// 执行无返回值的SQL操作
function execute_sql($sql){	
	global $_SGLOBAL;
	$_SGLOBAL['db']->query($sql);
}

// 更新usertolist表
function update_lsttousr($uid, $lids){

	if(get_rownum_with_sql("SELECT * FROM `tb_lsttousr` WHERE uid='$uid'"));
		execute_sql("DELETE FROM `tb_lsttousr` WHERE uid='$uid'");
	
	$lids = str_replace(" ", "", $lids);
	if (!$lids) return;
	
	$lids = split(",", $lids);
	$sql = "INSERT INTO `tb_lsttousr` (uid, lid) VALUES ";
	$arr = array();
	foreach($lids as $lid){
		$arr[] = "('$uid', '$lid')";
	}
	$sql = "INSERT INTO `tb_lsttousr` (uid, lid) VALUES " . implode(",", $arr);
	execute_sql($sql);
}


function addlog($description){
	date_default_timezone_set('Asia/Shanghai');//此句用于消除时间差
	$stime = date("Y-m-d H:i:s");
	$uname = $_SESSION["uname"];
	$time = date("Y-m-d H:i:s");
	execute_sql("INSERT INTO tb_log (uname,time,description) values ('$uname','$stime','$description')") ;
}

?>

